package com.motu.monstercity.server.game.logic;

import com.motu.monstercity.server.game.commondata.draw.TigerDraw;
import com.motu.vertx.module.utility.mysql.MysqlClient;
import com.motu.vertx.module.utility.mysql.MysqlTool;
import com.motu.vertx.module.utility.toolset.Tool;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.json.JsonArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class DrawBaseData {
    private static Logger logger = LoggerFactory.getLogger(DrawBaseData.class);
    private static ConcurrentHashMap<Integer, TigerDraw> tigerDrawMap = new ConcurrentHashMap<>(); // 老虎机抽奖

    // 公共数据初始化
    public static void init(List<Future> futures) {
        futures.add(initTigerDraw());
    }

    /**
     * 初始化老虎机抽奖
     */
    public static Future<Void> initTigerDraw() {
        String table = "tiger_draw";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, TigerDraw> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        TigerDraw entity = new TigerDraw(result.getJsonObject(i));
                        tmp.put(entity.getId(), entity);
                    }
                    tigerDrawMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static TigerDraw getTigerDraw(int id) {
        TigerDraw result = tigerDrawMap.get(id);
        if (result == null) {
            logger.error("getTigerDraw error key = " + id);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, TigerDraw> getTigerDrawMap() { return tigerDrawMap; }

    public static List<TigerDraw> getTigerDrawList() {
        List<TigerDraw> list = new ArrayList<>();
        for (TigerDraw tigerDraw : getTigerDrawMap().values())  {
            list.add(tigerDraw);
        }
        return list;
    }

    // 老虎机抽卡的概率列表
    public static int[] getTigerDrawRate() {
        ConcurrentHashMap<Integer, TigerDraw> list = getTigerDrawMap();
        int[] rates = new int[list.size()];
        int i = 0;
        for (TigerDraw tigerDraw : list.values())  {
            rates[i] = tigerDraw.getRate();
            i++;
        }
        return rates;
    }
}
